layui.use(['layer','table','jquery'],function(){

        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,

        table = layui.table;



    //用户列表
      var option={
        elem: '#userList',
        url : '/admin/travelBusiness/page',
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "userListTable",
        cols : [[
            {type: 'checkbox', fixed:'left',unresize:true},
            {field: 'name', title: '姓名',unresize:true,align:"center"},
            {field: 'sourcePlace',unresize:true, title: '出发地', align:"center"},
            {field: 'destination',unresize:true, title: '目的地', align:"center"},
            {field: 'reason', title: '出差缘由',unresize:true,  align:"center"},
            {field: 'transport', title: '交通工具',unresize:true,  align:"center"},
            {field: 'spent', title: '花费',unresize:true, align:'center'},
            {field: 'departTime',unresize:true, title: '出发时间',align:'center',sort:true,
                templet:function(d){
                    //处理时间
                    var date = new Date(d.departTime);
                    if(new Date()-date<0){
                        return '<b style="color: #ff1721">'+date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()+'</b>'
                    }
                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                }
            },
            {field: 'backTime', title: '返回时间',unresize:true,align:'center',sort:true,
                templet:function(d){
                    //处理时间
                    var date = new Date(d.backTime);
                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                }
            },
            {field: '88',title: '操作', minWidth:175, templet:'#tableListBar',fixed:"right",align:"center"}
        ]]
    }

    var tableIns = table.render(option);








    //搜索产品批次信息
    $(".search_btn").on("click",function(){
        var searchKey = $(".searchVal").val();
        if(searchKey != ''){
            option.url = '/admin/travelBusiness/page';
            option.method = 'post';
            option.where = {keyword: searchKey};
            table.render(option);
        }else{
            // layer.msg("请输入搜索的内容");
            tableIns.reload();
        }
    });

    //添加用户
    function addUser(edit){
        var index = layui.layer.open({
            title : "添加记录",
            type : 2,
            area:['100%','100%'],
            content : "travelBusinessAdd.jsp",
            success : function(layero, index){

            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    const IntervalId = setInterval(() => {
        if ($(".addNews_btn").length != 0) {
    $(".addNews_btn").click(function(){
        addUser();
    });


    //批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('userListTable'),
            data = checkStatus.data,
            newsId = [];
        if (data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].Id);
            }
            layer.confirm('确定删除选中的记录？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("/admin/travelBusiness/batchDelete", {
                    ids: newsId  //将需要删除的newsId作为参数传入
                }, function (data) {
                    if (data.code == 200) {
                        layer.msg("删除成功！");
                    } else {
                        layer.msg("删除失败，请重试！");
                    }
                    tableIns.reload();
                    layer.close(index);
                })
            })
        } else {
            layer.msg("请选择需要删除的记录");
        }
    })
        clearInterval(IntervalId);
    }
},0);





    //列表操作
    table.on('tool(userList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'&& $('#edit').hasClass('layui-btn-disabled')=== false){ //编辑
            var index = layui.layer.open({
                title : "修改信息",
                type : 2,
                content : "travelBusinessEdit.jsp?id=" + data.id,
                area: ["100%","100%"],
                success : function(layero, index){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回记录列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500);
                }
            });
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            });
        } else if(layEvent === 'del'&& $('#del').hasClass('layui-btn-disabled')=== false){ //删除
            layer.confirm('确定删除此记录？',{icon:3, title:'提示信息'},function(index){
                $.get("/admin/travelBusiness/delete?id=" + data.id,function(data){
                    tableIns.reload();
                    layer.close(index);
                });
            });
        }
    });

});